// import { pa } from 'element-plus/es/locale'
import { createStore } from 'vuex'
import request from '../assets/js/request'
import docCookies from '../assets/js/cookieutil'
// import { ElMessage } from 'element-plus'

export default createStore({
  state: {
    user: {
      nickname: '请登录',
      isLogin: false,
      group:{groupId:0}
    },
    isLogin: false,
    routePath: '/',
    area: {
      collegeId:0,
      collegeName:'全国'
    },
    ajaxQueue:{
      length:0,
      finished:0
    }
  },
  getters: {
  },
  mutations: {
    updateUser(state, payload) {
      state.user = payload
    },
    updateArea(state,payload){
      state.area=payload
    }
  },
  actions: {
    updateUser(context) {
      return new Promise((resolve, reject) => {
        if (context.state.isLogin) {
          resolve(context.state.user)
          return
        }
        let localToken = localStorage.getItem('access_token')
        if (localToken == null || localToken == ''||!docCookies.hasItem("cic-token")) {
          reject('用户未登录')
          return
        }
        request.get('/user/get_login_info').then(data => {
          if (data.success) {
            context.commit('updateUser', data.data)
            context.commit('updateArea', {
              collegeId:data.data.belong.split(',')[2],
              collegeName:data.data.belongCollegeName
            })
            resolve(data.data)
          } else {
            reject('未知错误: 在store/index.js')
          }
        }).catch(err => alert('网络错误：' + err))
      })
    },
  },
  modules: {
  }
})
